
/*
客户端
1)发送给服务器获取参数请求    
mobile monitor robin 123
服务器响应
yes | no

2)发送给服务器参数请求
mobile monitor_info monitor_info monitor_info
服务器响应
18#32#101

3)发送给服务器结束参数请求
mobile stop_moninfo_send stop_moninfo_send  stop_moninfo_send
服务器响应
无





*/

#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>			/* See NOTES */
#include <sys/socket.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>

#define SIZE 1024
int fd;


int main(void)
{

	int ret;
	
	//1.买电话
	fd = socket(AF_INET, SOCK_STREAM, 0);
	if (fd == -1) {
		perror("socket");
	}
#if 0	
	//2.绑卡
	struct sockaddr_in client; //自己ip+port
	client.sin_family = AF_INET;
	client.sin_port = htons(12346);
	client.sin_addr.s_addr = inet_addr("192.168.10.242");
	ret = bind(fd, (struct sockaddr *)&client, sizeof(client));
	if (ret == -1) {
		perror("bind");
	}
#endif
	//3.打电话
	struct sockaddr_in server; //访问服务器ip+port对象
	server.sin_family = AF_INET;
	server.sin_port = htons(12345);
	server.sin_addr.s_addr = inet_addr("192.168.10.242");
	ret = connect(fd, (struct sockaddr *)&server, sizeof(server));
	if (ret == -1) {
		perror("connect");
	}
	system("netstat -na | grep 12345"); //查看tcp连接状态

	//4.通信
	char buf[SIZE];
	char active = 0;
	int cnt =0;
	//1)发送给服务器获取参数请求
	printf("发送给服务器获取参数请求\n");
	send(fd, "mobile monitor robin 123", strlen("mobile monitor robin 123"), 0);

	bzero(buf, SIZE);
	recv(fd, buf, sizeof(buf), 0);
	if (strncmp("yes", buf, strlen(buf)) == 0) active = 1; 
	
	//2)发送给服务器参数请求
	while (active) {
		send(fd, "mobile monitor_info monitor_info monitor_info", 
			strlen("mobile monitor_info monitor_info monitor_info"), 0);
		bzero(buf, SIZE);
		ret = recv(fd, buf, sizeof(buf), 0);
		if (ret < 0 ) {
			printf("read error");
			exit(1);
		}
		cnt++;
		printf("%d> %s\n", cnt, buf);
		sleep(1);
		if (cnt == 10) active = 0;
		
	}

	//3)发送给服务器结束参数请求
	printf("发送给服务器结束参数请求\n");
	send(fd, "mobile stop_moninfo_send stop_moninfo_send  stop_moninfo_send", 
		strlen("mobile stop_moninfo_send stop_moninfo_send  stop_moninfo_send"), 0);
	sleep(1);
	//5.挂电话
	close(fd);
	return 0;
}
